#coding:utf-8
#Author Robin Wang Li xin
#Email:Luobintianya@gmail.com
#copyright: datiandou.com
from django.http import HttpRequest
from django.http import HttpResponse
from django.template import Context, loader, RequestContext
from django.template.loader import get_template
from django.shortcuts import render, get_object_or_404, render_to_response, HttpResponseRedirect
from django import http
from django.views.decorators.csrf import csrf_exempt, csrf_protect
from datiandouweb.models import DUploadFile,UserProfile,CommReg,Resume,Language,Status
from datiandouweb.forms import  FileUploadCvForm
import json
from django.core.files.uploadedfile import UploadedFile
from datiandou import settings
import uuid
import logging
import os
from django.db import transaction
from django.http import HttpResponse, HttpResponseBadRequest
logger = logging.getLogger(__name__)
logger = logging.getLogger(__name__)
from django.db.models import Q
from datiandouweb.forms import TaskForm
from datiandouweb.models import Task,Language,UserProfile
from datiandouweb import fwsutils

@csrf_exempt
def load_translate_task(request):
    userId=request.session.get("userID")
    return fwsutils.fws_response('taskcenter/tranlsateTaskWizard.html',request)



@csrf_exempt
def load_task_calendar(request):
    userId=request.session.get("userID")

    return  fwsutils.fws_response('taskcenter/calendar/calendar.html',request)





@csrf_exempt
def load_create_task(request):
    userId=request.session.get("userID")
    userInfo = UserProfile.objects.get(User_id=userId)
    language=CommReg.objects.filter(Type='selectbox',Status='0',schema='cv_language',User_id='system')
    return  fwsutils.fws_response('taskcenter/createtask.html',request,{'language':language,'userInfo':userInfo})





@transaction.autocommit
@csrf_exempt
def create_task(request):
    userId=request.session.get("userID")
    userInfo = UserProfile.objects.get(User_id=userId)
    task = TaskForm(request.POST)
    stateMessage=None
    if task.is_valid():
        content=task.cleaned_data['content']
        startdatepic = task.cleaned_data['startdatepic']
        enddatepic = task.cleaned_data['enddatepic']
        city =task.cleaned_data['city']
        language = task.cleaned_data['language']
        lang=Language.objects.get(pk=language)
        type = task.cleaned_data['type']
        price= task.cleaned_data['price']
        account =task.cleaned_data['account']
        cellphone = task.cleaned_data['cellphone']
        agree = task.cleaned_data['agree']
        datetask = Task()
        datetask.User_id = userInfo
        datetask.Task_name = content
        datetask.Task_start_time=startdatepic
        datetask.Task_end_time=enddatepic
        datetask.Task_address=city
        datetask.Prices=price
        datetask.Task_type=type
        datetask.Language=lang
        datetask.Task_user_name = account
        datetask.Task_user_cellphone = cellphone
        datetask.agree = agree
        datetask.Stage=1
        datetask.Status= Status.objects.get(Status_type=3,Status_key=1)
        try:
            print(task.cleaned_data['content'])
            if(datetask):
                datetask.save()
                stateMessage="任务创建成功!"
            else:
                stateMessage="任务创建失败!"
                print(datetask.errors)
        except Exception, e:
            transaction.rollback()
            stateMessage="任务创建失败!"
            print(e)
        print(stateMessage)
        request.session['stateMessage'] = stateMessage
    return  fwsutils.fws_redirect_response('../infocenter/load_infocenter', request,{'stateMessage': stateMessage})

@csrf_exempt
def view_task(request):

    userId=request.session.get("userID")
    userInfo = UserProfile.objects.get(User_id=userId)
    mytask = Task.objects.filter(User_id=userInfo)
    return fwsutils.fws_response('taskcenter/viewtask.html', request)




@csrf_exempt
def view_task_flow(request):
    userId=request.session.get("userID")
    userInfo = UserProfile.objects.get(User_id=userId)
    tasks = Task.objects.filter(User_id=userInfo)
    task_type=CommReg.objects.filter(Type='selectbox',Status='0',schema='task_flow',User_id='system')
    return fwsutils.fws_response('taskcenter/viewtaskflow.html',request,{'tasktype':task_type,'userInfo':userInfo,'tasks':tasks})
